package com.dev.security.manager.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import com.dev.security.manager.beans.App;
import com.dev.security.manager.beans.Permission;
import com.dev.security.manager.beans.Role;
import com.dev.security.manager.beans.RoleAppUnit;
import com.dev.security.manager.beans.RoleAppUnitId;
import com.dev.security.manager.beans.RolePermUnit;
import com.dev.security.manager.beans.RolePermUnitId;
import com.dev.security.manager.dao.AccountRoleUnitRepository;
import com.dev.security.manager.dao.AppRepository;
import com.dev.security.manager.dao.PermissionRepository;
import com.dev.security.manager.dao.RoleAppUnitRepository;
import com.dev.security.manager.dao.RolePermUnitRepository;
import com.dev.security.manager.dao.RoleRepository;
import com.dev.security.manager.service.IRoleService;

/**
 * @author 潜行的青衣
 * @see
 */
@Service
public class RoleService implements IRoleService {

	@Autowired
	private RoleRepository roleRepository;
	
	@Autowired
	private RoleAppUnitRepository roleAppUnitRepository;
	
	@Autowired
	private RolePermUnitRepository rolePermUnitRepository;
	
	@Autowired
	private AppRepository appRepository;
	
	@Autowired
	private PermissionRepository permissionRepository;
	
	@Autowired
	private AccountRoleUnitRepository accountRoleUnitRepository;
	
	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IRoleService#findAll()
	 */
	@Override
	public List<Role> findAll() {
		Sort sort = new Sort(new Order(Direction.ASC, "index"));
		return this.roleRepository.findAll(sort);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IRoleService#create(com.dev.security.manager.beans.Role)
	 */
	@Override
	public boolean create(Role role) {
		if(this.roleRepository.count(role.getId()) > 0)
			return false;
		this.roleRepository.save(role);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IRoleService#update(com.dev.security.manager.beans.Role)
	 */
	@Override
	public void update(Role role) {
		this.roleRepository.save(role);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IRoleService#remove(java.lang.String[])
	 */
	@Override
	public void remove(String[] role_ids) {
		if(role_ids != null) {
			for(String role_id:role_ids) {
				//删除角色与账户关联
				this.accountRoleUnitRepository.delete(role_id);
				//删除角色与app关联
				this.roleAppUnitRepository.delete(role_id);
				//删除角色与权限关联
				this.rolePermUnitRepository.delete(role_id);
				//删除角色
				this.roleRepository.delete(role_id);
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IRoleService#buildAppPermTree(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> buildAppPermTree(String role_id) {
		List<Map<String, Object>> tree = new ArrayList<Map<String, Object>>();
		Map<String, Object> root = new LinkedHashMap<String, Object>();
		root.put("id", "root");
		root.put("type", "root");
		root.put("text", "应用权限树");
		root.put("state", "open");
		
		Sort sort = new Sort(new Order(Direction.ASC, "index"));
		List<String> per_ids = this.rolePermUnitRepository.findAllPerId(role_id);
		Iterator<App> it = this.appRepository.findAll(sort).iterator();
		List<Map<String, Object>> apps = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> perms = null;
		Iterator<Permission> pit = null;
		Map<String, Object> appMap = null;
		Map<String, Object> permMap = null;
		App app = null;
		Permission perm = null;
		while(it.hasNext()) {
			app = it.next();
			appMap = new LinkedHashMap<String, Object>();
			appMap.put("id", app.getId());
			appMap.put("type", "app");
			appMap.put("text", app.getName());
			pit = this.permissionRepository.findAll(app.getId(), sort).iterator();
			perms = new ArrayList<Map<String, Object>>();
			appMap.put("children", perms);
			while(pit.hasNext()) {
				perm = pit.next();
				permMap = new LinkedHashMap<String, Object>();
				permMap.put("id", perm.getId());
				permMap.put("text", perm.getName());
				permMap.put("type", "permission");
				permMap.put("checked", per_ids.contains(perm.getId()));
				perms.add(permMap);
			}
			if(perms.size() > 0) {
				appMap.put("state", "closed");
			}
			apps.add(appMap);
		}
		root.put("children", apps);
		tree.add(root);
		return tree;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IRoleService#saveAppUnit(java.lang.String, java.lang.String[])
	 */
	@Override
	public void saveAppUnit(String role_id, String[] app_ids) {
		this.roleAppUnitRepository.delete(role_id);
		List<RoleAppUnit> list = new ArrayList<RoleAppUnit>();
		RoleAppUnit rau = null;
		for(String app_id:app_ids) {
			rau = new RoleAppUnit();
			rau.setId(new RoleAppUnitId(role_id, app_id));
			rau.setApp(new App(app_id));
			list.add(rau);
		}
		this.roleAppUnitRepository.save(list);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IRoleService#savePermUnit(java.lang.String, java.lang.String[])
	 */
	@Override
	public void savePermUnit(String role_id, String[] per_ids) {
		this.rolePermUnitRepository.delete(role_id);
		List<RolePermUnit> list = new ArrayList<RolePermUnit>();
		RolePermUnit rau = null;
		for(String app_id:per_ids) {
			rau = new RolePermUnit();
			rau.setId(new RolePermUnitId(role_id, app_id));
			rau.setPermission(new Permission(app_id));
			list.add(rau);
		}
		this.rolePermUnitRepository.save(list);
	}
	
}
